
##################
### Build maps ###
##################

library(sf)
library(plyr)
library(viridis)
library(ggrepel)
library(dplyr)

setwd("~/Dropbox (Princeton)/Research/*Spatial_Inequality/paper_SMD_inequality/JoP_final/replication")  

### Inequality data
load("data/inequality.RData")

ineq_2019 <- unique(inequality[ year == "2019" ,.(pcon, classification, diff_mean_med_tot)])

pcon_shp <- sf::st_read("data/shapefiles/pcon_shp") %>%
  dplyr::rename(pcon = pcon17cd) %>%
  left_join(ineq_2019)

### All
(p1 <- ggplot()
  + geom_sf(data = pcon_shp, mapping=aes(fill=log(diff_mean_med_tot)), colour="NA")
  + coord_sf()
  + theme_minimal()
  + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          axis.title.x=element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
          axis.title.y=element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(),
          legend.position = "bottom", legend.text = element_text(size=8), legend.key.size = unit(0.5, "cm"))
  + scale_fill_gradientn(name="Mean-median inequality, log £", colours=rev(magma(6)),
                         limits = c(8,12), breaks=seq(8,12,1),
                         guide = guide_legend(keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"),
                                              label.position = "bottom", title.position = 'top', nrow=1))
)

### London only
LOND_pcon_shp <- sf::st_read("data/shapefiles/pcon_shp") %>%
  dplyr::rename(pcon = pcon17cd) %>%
  right_join(ineq_2019[ !is.na(classification) ])

(p2 <- ggplot()
  + geom_sf(data = LOND_pcon_shp, mapping=aes(fill=log(diff_mean_med_tot)), colour="NA")
  + coord_sf()
  + theme_minimal()
  + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          axis.title.x=element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
          axis.title.y=element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(),
          legend.position = "bottom", legend.text = element_text(size=8), legend.key.size = unit(0.5, "cm"))
  + scale_fill_gradientn(name="Mean-median inequality, log £", colours=rev(magma(6)),
                         limits = c(8,12), breaks=seq(8,12,1),
                         guide = guide_legend(keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"),
                                              label.position = "bottom", title.position = 'top', nrow=1))
  + ggtitle("Greater London")
)


pdf("out/fig4.pdf", width=7, height=7) # map_diff_mean_med_tot_2019_UK_Lond_comb.pdf
ggpubr::ggarrange(p1,p2, ncol=2, nrow=1, common.legend = TRUE, legend="bottom", widths=c(1.5,1))
dev.off()




